#19th of April 20120

import unittest

def gen(start):
	'''generate consequential number from start.'''
	if not start:
		start = 0
	else: start = int(start)
	while 1:
		yield start
		start += 1

def buzfiz(*tuplearray):
	'''decorator that take a list of tuple like (d, newv). decorated-func returns newv if the non-decorated-func returns a value divisible by d.'''
	def decorator(f):
		def newfunction(*args, **kargs):
			result = f(*args, **kargs)
			newresult = ""
			for d,newval in tuplearray:
				if result%d==0: newresult+=newval
			if newresult: return newresult
			else: return result
		return newfunction
	return decorator

class Test_decorator(unittest.TestCase):
	def test_simple(self):

		@buzfiz((2, "fooz"), (3, "dudz"))
		def f(n): return n

		self.assertEqual(f(2), "fooz")
		self.assertEqual(f(3), "dudz")
		self.assertEqual(f(6), "foozdudz")
		self.assertEqual(f(1), 1)

	def test_requirement(self):
		@buzfiz((3, "fizz"),(5, "buzz"))
		def f(g): return next(g)

		s = ""
		s1 = "4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, "

		g = gen(4)
		for i in range(12):
			s += str(f(g)) + ", "
		self.assertTrue(s==s1)
	
class Test_generator(unittest.TestCase):
	def test_goodgen(self):
		g = gen(0)
		for i in range(1000):
			self.assertEqual(next(g), i)
	def test_badgen(self):
		g = gen("")
		self.assertEqual(0,next(g))
		
		g = gen("asd")
		self.assertRaises(ValueError, next, g)
		
unittest.main()
